package com.ikangtai.shecare.curve.mpchart;


import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.al.DayUnitDSOutput;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class SafePeriod {
    /**
     * BBTSafePeriodStart 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为安全期开始的下标位置position,
     * BBTSafePeriodEnd 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为安全期结束的下标位置position.
     */
    private static List<Integer> BBTSafePeriodStart = new ArrayList<>();
    private static List<Integer> BBTSafePeriodEnd = new ArrayList<>();

    private static boolean inited = false;

    public static void init(){
        initBBTSafePeriodStartEndDayList();
        initBBTLineChartxValue2PeriodMap();
        inited = true;
    }

    private static void initBBTSafePeriodStartEndDayList(){
        BBTSafePeriodStart.clear();
        BBTSafePeriodEnd.clear();

        AppLog.i("BBTLineChart App.dayUnitDSOutputsList.size() = " + App.dayUnitDSOutputsList.size());

        for(int i = 0; i < App.dayUnitDSOutputsList.size(); i++){
            AppLog.i("BBTLineChart dayUnitDSOutputsList period = " + App.dayUnitDSOutputsList.get(i).getPeriod(App.status));

            float safePeriodXAxisValue = BBTLineUtil.getXAxisValue(App.dayUnitDSOutputsList.get(i).date);
            AppLog.i("BBTLineChart dayUnitDSOutputsList date = " + DateUtil.getSimpleCircleCalendarTopDate(App.dayUnitDSOutputsList.get(i).date) +
                    ", position = " + i + ", safePeriodXAxisValue = " + safePeriodXAxisValue);

            if(isSafePeriodStart(i) && BBTLineUtil.isValidXAxisValue(safePeriodXAxisValue) || isFirstXAxisValueSafePeriod(safePeriodXAxisValue, i))
                BBTSafePeriodStart.add(i);

            if(isSafePeriodEnd(i) && BBTLineUtil.isValidXAxisValue(safePeriodXAxisValue) || isFinalXAxisValueSafePeriod(safePeriodXAxisValue, i))
                BBTSafePeriodEnd.add(i);
        }
    }

    private static boolean isSafePeriodStart(int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        if(position == 0 && period.equals(DayUnitDSOutput.PERIOD_AQQ)) return true;
        if(position == 0 && !period.equals(DayUnitDSOutput.PERIOD_AQQ)) return false;

        DayUnitDSOutput previousDayUnitDSOutput = App.dayUnitDSOutputsList.get(position - 1);
        String previousDayPeriod = previousDayUnitDSOutput.getPeriod(App.status);

        if(period.equals(DayUnitDSOutput.PERIOD_AQQ) &&
                !previousDayPeriod.equals(DayUnitDSOutput.PERIOD_AQQ))
            return true;
        else
            return false;
    }

    private static boolean isSafePeriodEnd(int position){

        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        AppLog.i("BBTLineChart isSafePeriodEnd position = " + position + ", period = " + period +
        ", result = " + (position == App.dayUnitDSOutputsList.size() - 1 && period.equals(DayUnitDSOutput.PERIOD_AQQ)));

        if(position == App.dayUnitDSOutputsList.size() - 1 && period.equals(DayUnitDSOutput.PERIOD_AQQ)) return true;
        if(position == App.dayUnitDSOutputsList.size() - 1 && !period.equals(DayUnitDSOutput.PERIOD_AQQ)) return false;

        DayUnitDSOutput NextDayUnitDSOutput = App.dayUnitDSOutputsList.get(position + 1);
        String nextDayPeriod = NextDayUnitDSOutput.getPeriod(App.status);

        if(period.equals(DayUnitDSOutput.PERIOD_AQQ) &&
                !nextDayPeriod.equals(DayUnitDSOutput.PERIOD_AQQ))
            return true;
        else
            return false;
    }

    /**
     * 刚好曲线X轴的第一天为处于安全期，但是在 dayUnitDSOutputsList 中是安全期的中间位置，非起始位置
     * @param safePeriodXAxisValue
     * @param position
     * @return
     */
    private static boolean isFirstXAxisValueSafePeriod(float safePeriodXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return safePeriodXAxisValue == BBTLineChart.MIN_XAXIS_DAY && period.equals(DayUnitDSOutput.PERIOD_AQQ);
    }

    /**
     * 刚好曲线X轴的最后一天为处于安全期，但是在 dayUnitDSOutputsList 中是安全期的中间位置，非末位置
     * @param safePeriodXAxisValue
     * @param position
     * @return
     */
    private static boolean isFinalXAxisValueSafePeriod(float safePeriodXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return safePeriodXAxisValue == BBTLineChart.MAX_XAXIS_DAY && period.equals(DayUnitDSOutput.PERIOD_AQQ);
    }

    public static void initBBTLineChartxValue2PeriodMap(){
        for(int i = 0; i < BBTSafePeriodStart.size(); i++){
            int safePeriodStartPoisition = BBTSafePeriodStart.get(i);
            int safePeriodEndPoisition = BBTSafePeriodEnd.get(i);

            AppLog.i("BBTLineChart Safe background, i = " + i + ", safePeriodStartPoisition = " + safePeriodStartPoisition + ", safePeriodEndPoisition = " + safePeriodEndPoisition);

            initSafeXValue2PeriodMap(safePeriodStartPoisition, safePeriodEndPoisition);
        }
    }

    private static void initSafeXValue2PeriodMap(int safePeriodStartPoisition, int safePeriodEndPoisition){
        for(int safePeriodPoistion = safePeriodStartPoisition; safePeriodPoistion <= safePeriodEndPoisition; safePeriodPoistion ++){
            DayUnitDSOutput safePeriodDayUnitDSOutput = App.dayUnitDSOutputsList.get(safePeriodPoistion);
            float safePeriodXAxisValue = BBTLineUtil.getXAxisValue(safePeriodDayUnitDSOutput.date);

            AppLog.i("BBTLineChart safePeriodXAxisValue = " + DateUtil.getSimpleCircleCalendarTopDate(safePeriodDayUnitDSOutput.date) + ", x = " + BBTLineUtil.getXAxisValue(safePeriodDayUnitDSOutput.date));

            BBTLineChart.bbtLineChartxValue2PeriodMap.put(safePeriodXAxisValue, DayUnitDSOutput.PERIOD_AQQ);
        }
    }

    public static boolean isInSafePeriod(long daySeconds){
//        boolean inSafePeriod = false;
//
//        for(int i = 0; i < BBTSafePeriodStart.size(); i++) {
//            int safePeriodStartPoisition = BBTSafePeriodStart.get(i);
//            DayUnitDSOutput safePeriodStartDayUnitDSOutput = App.dayUnitDSOutputsList.get(safePeriodStartPoisition);
//
//            int safePeriodEndPoisition = BBTSafePeriodEnd.get(i);
//            DayUnitDSOutput safePeriodEndDayUnitDSOutput = App.dayUnitDSOutputsList.get(safePeriodEndPoisition);
//
//            if(daySeconds >= safePeriodStartDayUnitDSOutput.date - 12 * 3600 && daySeconds <= safePeriodEndDayUnitDSOutput.date + 12 * 3600){
//                inSafePeriod = true;
//                break;
//            }
//        }
//
//        return inSafePeriod;

        float xAxisValue = BBTLineUtil.getXAxisValue(daySeconds);
        return BBTLineChart.bbtLineChartxValue2PeriodMap.get(xAxisValue) == DayUnitDSOutput.PERIOD_AQQ;
    }

    public static List<Integer> getBBTSafePeriodStartList(){
        if(!inited)
            init();

        return BBTSafePeriodStart;
    }

    public static List<Integer> getBBTSafePeriodEndList(){
        if(!inited)
            init();

        return BBTSafePeriodEnd;
    }
}
